import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const state = {
  token: getToken(),
  name: '',
  avatar: '',
  platForm: ''
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
    console.log("state.token:",state.token)
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_PLATFORM: (state, platForm) => {
    state.platForm = platForm
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    console.log(userInfo)
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ userName: username.trim(), password: password }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.token)
        setToken(data.token)
        console.log("Login:","登录成功!")
        console.log("Login:",data.token)
        resolve()
      }).catch(error => {
        reject(error)
        console.log("Login:","登录失败!")
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
   /* return new Promise((resolve, reject) => {
        const data = {
          roles: ['admin'],
          introduction: 'I am a super administrator',
          avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
          name: 'Super Admin'
        }
      if (!data) {
        reject('Verification failed, please Login again.')
      }

      const { name, avatar } = data

      commit('SET_NAME', name)
      commit('SET_AVATAR', avatar)
      resolve(data)
    })*/
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response
        if (!data) {
          reject('Verification failed, please Login again.')
        }

        console.log("getInfo:",data)
        const { name, id ,webPageEntities,countryUploadStatus,provinceUploadStatus,cityUploadStatus,snInfos} = data
        sessionStorage.setItem("userId",id)
        sessionStorage.setItem("userName",name)
        sessionStorage.setItem("quanXian",JSON.stringify(webPageEntities))
        sessionStorage.setItem("gateWayJson",JSON.stringify(snInfos))

        sessionStorage.setItem("uploadStatus",JSON.stringify([countryUploadStatus,provinceUploadStatus,cityUploadStatus]))
        commit('SET_NAME',name)
        commit('SET_AVATAR', 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif')
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      commit('SET_TOKEN', '')
      commit('SET_NAME','')
      removeToken()
      resetRouter()
      resolve()
      /*logout(state.token).then(() => {
        commit('SET_TOKEN', '')
        removeToken()
        resetRouter()
        resolve()
      }).catch(error => {
        reject(error)
      })*/
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      removeToken()
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

